const app = getApp();
Component({
    /**
     * 组件的属性列表
     */
    properties: {

    },

    /**
     * 组件的初始数据
     */
    data: {
        searchValue: "",
        waitDeliverList: [],
        isLastPage: false,
        current: 1,
        size: 10,
    },

    lifetimes: {
        attached: function () {
            // 在组件实例进入页面节点树时执行
            this.getData()
        },
        detached: function () {
            // 在组件实例被从页面节点树移除时执行
        },
    },

    pageLifetimes: {
        show: function () {
            this.onPullDownRefresh()
        },
    },

    /**
     * 组件的方法列表
     */
    methods: {
        onPullDownRefresh() {
            this.setData({
                current: 1,
                searchValue: "",
                waitDeliverList: [],
                isLastPage: false
            })
            this.getData()
        },
        onReachBottom() {
            let {
                current,
                isLastPage
            } = this.data
            if (isLastPage) {
                return
            }
            this.setData({
                current: (Number(current) + 1)
            })
            this.getData()
        },
        syncToAccountBook(e) {
            let item = e.currentTarget.dataset.item
            item.orderType = 1
            item.type = 0
            item.financeName = "卖菜"
            this.triggerEvent('bookPicker', item)
        },
        toDetail(e) {
            const item = e.currentTarget.dataset.item
            wx.navigateTo({
                url: `/pages/dashboard/hzdetail/hzdetail?id=${item.id}&type=2`
            })
        },
        onSearch() {
            this.setData({
                current: 1,
                waitDeliverList: [],
                isLastPage: false
            })
            this.getData()
        },
        getData() {
            const {
                current,
                searchValue,
                size
            } = this.data;
            let params = {
                current,
                size,
                cropName: searchValue,
                outStockStatus: 3,
                companyId: app.globalData.userInfo.companyData.companyId
            }
            wx.showLoading({
                title: "正在请求数据中"
            })
            wx.$apis.marketerPage(params).then(res => {
                wx.stopPullDownRefresh();
                let oldWaitDeliverList = this.data.waitDeliverList;
                let waitDeliverList = res.data && res.data.records.map(item => {
                    item.outStockTime = item.outStockTime && item.outStockTime.replace("T", " ")
                    item.packPrice = item.packPrice && wx.$getYuan(item.packPrice)
                    item.serverPrice = item.serverPrice && wx.$getYuan(item.serverPrice)
                    return item
                })
                if (waitDeliverList && waitDeliverList.length) {
                    let newWaitDeliverList = oldWaitDeliverList.concat(waitDeliverList)
                    this.setData({
                        waitDeliverList: newWaitDeliverList
                    })
                    if (waitDeliverList.length < size) {
                        this.setData({
                            isLastPage: true
                        })
                    }
                } else {
                    this.setData({
                        isLastPage: true
                    })
                }
            }).catch(err => {
                wx.stopPullDownRefresh();
            })
        }
    }
})